package psn.zjk.trying;

import org.drools.example.api.namedkiesession.Message;
import org.kie.api.cdi.KReleaseId;
import org.kie.api.cdi.KSession;
import org.kie.api.runtime.KieSession;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Component;
import psn.zjk.ruleGrp1.Order;

import javax.annotation.PostConstruct;

@Component
public class KieHelper extends ApplicationObjectSupport {
//    @KContainer
//    @KReleaseId(groupId = "jar1", artifactId = "art1", version = "1.1")
//    private KieContainer kContainer;

//    @KBase
//    @KReleaseId(groupId = "psn.zjk", artifactId = "kjar", version = "0.0.1-SNAPSHOT")
//    private KieBase kbase;

    @KSession("zz")
//    @KSession("zz")
//    @KReleaseId(groupId = "psn.zjk", artifactId = "kjar", version = "0.0.1-SNAPSHOT")
    private KieSession kieSession3;
    @KSession("ksession1")
    @KReleaseId(groupId = "org.drools", artifactId = "named-kiesession", version = "7.62.0.Final")
    private KieSession kSession2;

    @PostConstruct
    public void run() {
//        KieServices ks = KieServices.Factory.get();
//        KieContainer kContainer = ks.newKieContainer(
//                ks.newReleaseId("org.mygroup", "myartifact", "1.0") );

//        KieSession kSession = kContainer.newKieSession("ksession1");
        //构造订单对象，设置原始价格，由规则引擎根据优惠规则计算优惠后的价格
        Order order = new Order();
        order.setOriginalPrice(210D);
        Order order1 = new Order();
        order1.setOriginalPrice(10D);
        Order order2 = new Order();
        order2.setOriginalPrice(110D);
        Order order3 = new Order();
        order3.setOriginalPrice(510D);

        //将数据提供给规则引擎，规则引擎会根据提供的数据进行规则匹配
        kieSession3.insert(order);

        kieSession3.insert(order1);
        kieSession3.insert(order2);
        kieSession3.insert(order3);

        kieSession3.fireAllRules();
        System.out.println("优惠前原始价格：" + order.getOriginalPrice() +
                "，优惠后价格：" + order.getRealPrice());
        System.out.println("优惠前原始价格：" + order1.getOriginalPrice() +
                "，优惠后价格：" + order1.getRealPrice());
        System.out.println("优惠前原始价格：" + order2.getOriginalPrice() +
                "，优惠后价格：" + order2.getRealPrice());
        System.out.println("优惠前原始价格：" + order3.getOriginalPrice() +
                "，优惠后价格：" + order3.getRealPrice());



        kSession2.setGlobal("out", System.out);
        kSession2.insert(new Message("Dave", "Hello, AAA. Do you read me, AAAA?"));
        kSession2.fireAllRules();

        kSession2.insert(new Message("Dave", "Hello, HAL. Do you read me, HAL?"));
        kSession2.fireAllRules();
    }

//    @PostConstruct
//    public void scan() {
//        KieScanner releaseIdScanner = obtainApplicationContext().getBean("namedKieSession#scanner", KieScanner.class);
//        releaseIdScanner.scanNow();
//    }


}
